#ifndef CPP_FAST_LISTVIEW_H
#define CPP_FAST_LISTVIEW_H

#include <Font.h>
#include <Bitmap.h>
#include <widget/AbsListView.h>

class FastListView;

//@brief 快速列表控件
//@author cvte
//@description 效果同列表控件，但优化列表绘制效率
class FastListViewResData {
   public:
    FastListViewResData();
    virtual ~FastListViewResData();

    //@method 设置区域
    //@param rect 矩形对象
    void setRect(Rect& rect);
    Rect* getRect();
    int getResType();
    void setId(int id);
    int getId();

   protected:
    int mResId = -1;
    Rect mRect;
    int mResType = 0;
};

class FastListViewResTextData : public FastListViewResData {
   public:
    FastListViewResTextData();
    virtual ~FastListViewResTextData();

    //@method 设置文本字体
    //@param font 字体对象
    void setTextFont(Font& font);
    Font* getTextFont();

    //@method 设置文本对齐风格
    //@param alignStyle 文本对齐风格
    void setTextAlignment(TextAlignStyle alignStyle);
    TextAlignStyle getTextAlignment();

    //@method 设置文本内容
    //@param text 文本内容字符串
    void setText(const char* text);
    const char* getText();

    //@method 设置文本颜色
    //@param color 颜色
    void setTextColor(int color);
    int getTextColor();

    //@method 设置高亮文本内容
    //@param text 高亮文本内容字符串
    void setHighlightText(const char* text);
    const char* getHighlightText();

    //@method 设置高亮文本颜色
    //@param color 颜色
    void setHighlightTextColor(int color);
    int getHeighlightTextColor();

   private:
    Font* mFont = nullptr;
    TextAlignStyle mTextAlignStyle = TEXT_ALIGN_NONE;
    std::string mText;
    int mTextColor = 0;
    std::string mHighlightText;
    int mHighlightTextColor = 0;
};

class FastListViewResBackgroundData : public FastListViewResData {
   public:
    FastListViewResBackgroundData();
    virtual ~FastListViewResBackgroundData();

    //@method 设置普通状态颜色
    void setColor(int color);
    //@method 获取普通状态颜色
    int getColor();
    //@method 设置高亮状态颜色
    void setHighlightColor(int color);
    //@method 获取高亮状态颜色
    int getHighlightColor();

   private:
    int mColor = 0;
    int mHighlightColor = 0;
};

class FastListViewResBitmapData : public FastListViewResData {
   public:
    FastListViewResBitmapData();
    virtual ~FastListViewResBitmapData();

    //@method 设置图片
    //@param bitmap 图片资源
    void setBitmap(Bitmap& bitmap);
    Bitmap* getBitmap();

    //@method 设置图片高亮状态
    //@param bitmap 图片资源
    void setHighlightBitmap(Bitmap& bitmap);
    Bitmap* getHighlightBitmap();

   private:
    Bitmap* mBitmap = nullptr;
    Bitmap* mBitmapHighlight = nullptr;
};

class FastListViewItemData {
   public:
    typedef std::vector<FastListViewResData*> ResDataVector;

    FastListViewItemData();
    virtual ~FastListViewItemData();
    void setId(int id);
    int getId();
    ResDataVector* getResDataVector();
    void setCurSelectedResData(FastListViewResData* res);
    FastListViewResData* getCurSelectedResData();

   private:
    int mId = -1;
    Rect mRect;
    ResDataVector mResDataVector;
    FastListViewResData* mCurSelectedResData = nullptr;
};

class FastListViewAdapter {
   public:
    class OnItemSelectedListener {
       public:
        virtual void onFastListViewItemSelected(FastListView& listView, FastListViewItemData& itemData);
    };

    typedef std::vector<FastListViewItemData*> ItemDataVector;

    FastListViewAdapter();
    virtual ~FastListViewAdapter();
    ItemDataVector* getItemDataVector();
    void setOnItemSelectedListener(OnItemSelectedListener& listener);
    OnItemSelectedListener* getOnItemSelectedListener();
    void clearItemDataVector();

   private:
    ItemDataVector mItemDataVector;
    OnItemSelectedListener* mOnItemSelectedListener = nullptr;
};

class FastListView : public AbsListView {
   public:
    FastListView();
    virtual ~FastListView();

    void setAdapter(FastListViewAdapter& adapter);
    FastListViewAdapter* getAdapter();
    void setItemRect(Rect& rect);
    Rect* getItemRect();
    //@method 设置选中项，并触发回调和动效
    void setSelection(int position);
    //@method 设置选中项，shouldTriggerCallback为true就触发回调和动效，否则不触发回调和动效
    void setSelection(int position, bool shouldTriggerCallback);
    int getSelection();

   private:
    int mSelection = -1;
    Rect mItemRect;
    FastListViewAdapter* mAdapter = nullptr;
    FastListViewItemData* mSelectedItem = nullptr;

    void onDraw(Canvas& canvas);
};

#endif /* CPP_FAST_LISTVIEW_H */